package com.xw.controller.xw;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xw.controller.base.BaseController;
import com.xw.entity.Page;
import com.xw.service.xw.caseOrder.CaseOrderService;
import com.xw.service.xw.patient.PatientService;
import com.xw.util.AppUtil;
import com.xw.util.DateUtil;
import com.xw.util.Jurisdiction;
import com.xw.util.PageData;
/**
 * 类名称: PatientController
 * 类描述: 患者controller
 * @author : gce
 * @date   : 2017-11-30
 */
@Controller
@RequestMapping(value="/patient")
public class PatientController extends BaseController {
	
	@Resource(name="patientService")
	private PatientService patientService ;
	@Resource(name="caseOrderService")
	private CaseOrderService caseOrderService;
	
	/**
	 * 今日到诊患者列表#{pd.name}#{pd.phone}
	 * @param page
	 * @throws Exception
	 * @return: ModelAndView
	 * @author: gce
	 * @date  : 2017-12-4
	 */
	@RequestMapping(value = "/todayPatients")
	public ModelAndView todayPatients(Page page) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("isToday", "yes");
		page.setPd(pd);
		List<PageData> todayPatients = patientService.todaylistPage(page);
		for (PageData pageData : todayPatients) {
			if(pageData.get("birth")!=null && pageData.get("birth")!=""){
				int age = DateUtil.getAge((Date)pageData.get("birth"));
				pageData.put("age", age);
			}
		}
		mv.addObject("todayPatients", todayPatients);
		mv.addObject("pd", pd);
		mv.setViewName("xw/patient/todayPatients");
		return mv ;
	}

	
	/**
	 * 跳转到添加患者页面
	 * @return mv
	 * @return: ModelAndView
	 * @author: gce
	 * @date  : 2017-11-30
	 */
	@RequestMapping(value="/goAddP")
	public ModelAndView goAddP(){
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("xw/patient/addPatient");
		return mv;
	}
	
	/**
	 * 跳转到修改患者页面
	 * @return mv
	 * @return: ModelAndView
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2017-11-30
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = patientService.findById(pd);
		int age = DateUtil.getAge((Date)pd.get("birth"));
		pd.put("age", age);
		mv.addObject("pd", pd);
		mv.setViewName("xw/patient/addPatient");
		return mv;
	}
	
	/**
	 * 判断用户是否存在：姓名电话
	 * @throws Exception 
	 */
	@RequestMapping(value = "/hasP")
	@ResponseBody
	public Object hasYp(HttpServletRequest request) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String msg = "";
		PageData pd = new PageData();
		pd = this.getPageData();
		Page page = new Page();
		page.setPd(pd);
		List<PageData> Patients = patientService.list(page);
		//根据患者姓名和电话验证患者是否已经存在,存在，就返回提示信息
		if(Patients.size()>0){
			msg="success";
		}
		map.put("result", msg); // 返回结果
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 添加患者/修改
	 * @return: Object
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2017-11-30
	 */
	@RequestMapping(value="/saveP")
	public ModelAndView saveP(){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String msg = "";
		try {
			String age = (String) pd.get("age");
			String birthYear = DateUtil.getBirthYear(Integer.parseInt(age));
			Date birth = DateUtil.fomatDate(birthYear+"-01-01");
			pd.put("birth", birth);
			if(pd.get("hzId")!=null && !pd.get("hzId").equals("")){
				pd.put("updateDate", new Date());
				int result = patientService.edit(pd);
				if(result>0){
					msg="success";
				}else{
					msg="操作失败";
				}
			}else{
				pd.put("createDate", new Date());
				pd.put("updateDate", new Date());
				int result = patientService.save(pd);
				if(result>0){
					msg="success";
				}else{
					msg="操作失败";
				}
			}
		} catch (Exception e) {
			logger.error(e.toString(),e);
		}
		mv.addObject("msg", msg);
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 所有患者列表（或根据姓名、或根据电话）
	 * @param page
	 * @throws Exception
	 * @return: ModelAndView
	 * @author: gce
	 * @date  : 2017-12-5
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(Page page) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		page.setPd(pd);
		List<PageData> allPatients = patientService.list(page);
		for (PageData pageData : allPatients) {
			if(pageData.get("birth")!=null && pageData.get("birth")!=""){
				int age = DateUtil.getAge((Date)pageData.get("birth"));
				pageData.put("age", age);
			}
		}
		mv.setViewName("xw/patient/allPatients");
		mv.addObject("allPatients", allPatients);
		mv.addObject("pd", pd);
		return mv ;
	}

	/**
	 * 删除患者
	 */
	@RequestMapping(value = "/deleteP")
	@ResponseBody
	public Object deleteP(){
		Map<String, String> map = new HashMap<String, String>();
		String msg = "success";
		PageData pd = new PageData();
		try {
			pd = this.getPageData();
			//判断是否有删除权限
				//判断该患者是否有正常状态的病例，若有，则不能删除
				Page page = new Page();
				page.setPd(pd);
				List<PageData> caseOrderList = caseOrderService.list(page);
				if(caseOrderList.size()<=0){
					int result = patientService.delete(pd);
					if(result>0){
						msg = "success";
					}else{
						logger.info("--删除失败--");
						msg = "--删除失败--";
					}
				}else{
					logger.info("--删除失败：有病例单--");
					msg = "--删除失败：有病例单--";
				}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			msg = "--删除异常--";
		}
		map.put("msg", msg);
		return AppUtil.returnObject(new PageData(), map);
	}
	
}
